package com.zhiyou.controller.SystemUserController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.User;
import com.zhiyou.service.SystemUserService.SystemUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shiro/user")
public class SystemUserController {

    @Autowired
    SystemUserService systemUserService;

    @RequestMapping("/menus")
    public String menusList(@RequestParam(name="pn",defaultValue = "1") Integer pn, Map<String,PageInfo> map){
        PageHelper.startPage(pn, 5 );
        ArrayList<User> userlist = systemUserService.selectAllUser();
        PageInfo page = new PageInfo( userlist, 5);
        map.put("pageInfo",page);
        userlist.stream().forEach(s->{
            System.out.println(s.toString());
        });
        return "usermenus";
    }

    @RequestMapping("/selectbyname")
    public String selectByname(@RequestParam(name="pn",defaultValue = "1") Integer pn, Map<String,PageInfo> map,String username){
        PageHelper.startPage(pn, 5 );
        ArrayList<User> userList  = systemUserService.selectByName(username);

        if (userList.isEmpty()){
            return "usernamefalse";
        }
        PageInfo page = new PageInfo(userList, 5);
        map.put("pageInfo",page);

        return "usermenus";
    }

    @RequestMapping("/delbyId")
    public String delById(@RequestParam(name="pn",defaultValue = "1") Integer pn, Map<String,PageInfo> map,User user){
        PageHelper.startPage(pn, 5 );
        System.out.println(user.getuId());
        int i= systemUserService.delUserByid(user);
        ArrayList<User> userList = systemUserService.selectAllUser();
        PageInfo page = new PageInfo(userList, 5);
        map.put("pageInfo",page);
        userList.stream().forEach(s->{
            System.out.println(s.toString());
        });

        return "usermenus";
    }

    @RequestMapping("useredit")
    public String useredit(User user ,HttpSession session){
        User user1 = systemUserService.selectEditInfo(user);
        session.setAttribute("user",user1);
        return "useredit";
    }

    @RequestMapping("userup")
    public String userUp(@RequestParam(name="pn",defaultValue = "1") Integer pn, Map<String,PageInfo> map,User user,Integer rId){
        PageHelper.startPage(pn, 5 );

        System.out.println("sdasd");
        System.out.println(user.toString());
        int i = systemUserService.updateUser(user,rId);
        System.out.println(i);
        ArrayList<User> userlist = systemUserService.selectAllUser();
        PageInfo page = new PageInfo(userlist, 5);
        map.put("pageInfo",page);
        userlist.stream().forEach(s->{
            System.out.println(s.toString());
        });
        return "usermenus";
    }
    @RequestMapping("userInsert")
    public String userInsert(@RequestParam(name="pn",defaultValue = "1") Integer pn, Map<String,PageInfo> map,User user,Integer rId){
        PageHelper.startPage(pn, 5 );

        int is=  systemUserService.userInsert(user,rId);

        ArrayList<User> userlist = systemUserService.selectAllUser();

        PageInfo page = new PageInfo(userlist, 5);

        map.put("pageInfo",page);

       return "usermenus";
    }

    //页面跳转功能
    @RequestMapping("useradd")
    public String useraddpage(){
        return "useradd";
    }

}
